import axios, { AxiosResponse } from "axios";
import { PaymentModel } from "../models/Payment";
import { UserModel } from "../models/User";
import { ElMessage } from "element-plus";

class PaymentApis {
    paymentOrder(payment: PaymentModel): Promise<boolean> {
        let promise: Promise<boolean> = new Promise((resolve, reject) => {
            axios.post('/api/payment/payment', payment)
                .then((result: AxiosResponse) => {
                    let code: number = result.data.code;
                    if (code == 200) {
                        resolve(true);
                    } else {
                        ElMessage.warning(result.data.error);
                        resolve(false);
                    }
                })
                .then((error) => {
                    reject(error);
                })
        });
        return promise;
    }

    recharge(user: UserModel): Promise<boolean> {
        let promise: Promise<boolean> = new Promise((resolve, reject) => {
            axios.post('/api/payment/recharge', user)
                .then((result: AxiosResponse) => {
                    let code: number = result.data.code;
                    if (code == 200) {
                        resolve(true);
                    } else {
                        resolve(false);
                    }
                })
                .then((error) => {
                    reject(error);
                })
        });
        return promise;
    }
}

export const paymentApis: PaymentApis = new PaymentApis();